﻿#include "CAlgorithmContext.h"
#include "CAlgorithmImageHy.h"
#include "CAlgorthmSensor.h"
#include "CAlgorithmVoiceHy.h"
CAlgorithmContext::CAlgorithmContext(QString sGuid)
{
	m_sGuid = sGuid;
}

CAlgorithmContext::~CAlgorithmContext()
{
	for (auto it = m_mapAlgorithm.begin(); it != m_mapAlgorithm.end(); it++)
	{
		delete it.value();
		it.value() = nullptr;
	}
	m_mapAlgorithm.clear();
}

void CAlgorithmContext::init(CDevBase * pIFDev, CRBaseData * pRBasicData)
{
	m_pDev = pIFDev;
	m_pRBasicDataNd = pRBasicData;
}

bool CAlgorithmContext::actionAlgorthm(SCPolItemCom * pPolItemCom, QString sTaskName)
{
	if (pPolItemCom == nullptr)
	{
		return false;
	}
	int nDevType = pPolItemCom->m_pPolItem->m_nDevTypeCode;//取出 巡检项设备类型编码  
	CAlgorithmBase *pAlgorithmBase = getAlgorithm(nDevType);
	if (pAlgorithmBase == nullptr)
	{
		return false;
	}
	return pAlgorithmBase->actionAlgorthm(pPolItemCom, sTaskName);  //算法处理
}

void CAlgorithmContext::closeCotDet()
{
	for (auto it = m_mapAlgorithm.begin(); it != m_mapAlgorithm.end(); it++)
	{
		it.value()->closeCotDet();
	}
}

CAlgorithmBase * CAlgorithmContext::getAlgorithm(int nDevType)
{
	if (m_mapAlgorithm.contains(nDevType))
	{
		return m_mapAlgorithm[nDevType];
	}
	else
	{
		int nSmallSceneType = m_pRBasicDataNd->m_nRobType;
		CAlgorithmBase *pAlgorithmBase = nullptr;
		switch (nDevType)
		{
		case enCO:
			pAlgorithmBase = new CAlgorthmSensor(m_sGuid);
			break;
		case enGas:
			pAlgorithmBase = new CAlgorthmSensor(m_sGuid);
			break;
		case enPM25:
			pAlgorithmBase = new CAlgorthmSensor(m_sGuid);
			break;
		case enPM10:
			pAlgorithmBase = new CAlgorthmSensor(m_sGuid);
			break;
		case enTemp:
			pAlgorithmBase = new CAlgorthmSensor(m_sGuid);
			break;
		case enHum:
			pAlgorithmBase = new CAlgorthmSensor(m_sGuid);
			break;
		case enUltrasonic:
			pAlgorithmBase = new CAlgorthmSensor(m_sGuid);
			break;
		case enCamer:
			if (nSmallSceneType == 0) //南电机器人
			{
				pAlgorithmBase = new CAlgorithmImageHy(m_sGuid);
			}
			else if (nSmallSceneType == 1) //发耳机器人
			{
				pAlgorithmBase = new CAlgorithmImageHy(m_sGuid);
			}
			else if (nSmallSceneType == 2) //虹洋机器人
			{
				pAlgorithmBase = new CAlgorithmImageHy(m_sGuid);
			}
			break;
		case enIR:
		{
			if (nSmallSceneType == 0) //南电机器人
			{
				pAlgorithmBase = new CAlgorithmIrHy(m_sGuid);
			}
			else if (nSmallSceneType == 1) //发耳机器人
			{
				pAlgorithmBase = new CAlgorithmIrHy(m_sGuid);
			}
			else if (nSmallSceneType == 2) //虹洋机器人
			{
				pAlgorithmBase = new CAlgorithmIrHy(m_sGuid);
			}
		}
		break;
		case enVoice:
		{
			if (nSmallSceneType == 0)  //南电机器人
			{
				pAlgorithmBase = new CAlgorithmVoiceHy(m_sGuid);
			}
			else if (nSmallSceneType == 1) //发耳机器人
			{
				pAlgorithmBase = new CAlgorithmVoiceHy(m_sGuid);
			}
			else if (nSmallSceneType == 2) //虹洋机器人
			{
				pAlgorithmBase = new CAlgorithmVoiceHy(m_sGuid);
			}

		}
		break;
		default:
			break;
		}

		if (pAlgorithmBase != nullptr)
		{
			pAlgorithmBase->init(m_pDev, m_pRBasicDataNd);
			m_mapAlgorithm.insert(nDevType, pAlgorithmBase);
		}
		return pAlgorithmBase;
	}
}
